import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:tianpu_edu/Classes/Home/Model/home_course_module_list.dart';
import 'package:tianpu_edu/Classes/Home/Model/home_detail.dart';
import 'package:tianpu_edu/Classes/Home/View/home_expansion_tile_list.dart';
import 'package:tianpu_edu/Classes/libs/tool_api_url.dart';
import 'package:tianpu_edu/Classes/libs/tool_base_page.dart';
import 'package:tianpu_edu/Classes/libs/tool_base_widget.dart';
import 'package:tianpu_edu/Classes/libs/tool_color_utils.dart';
import 'package:tianpu_edu/Classes/libs/tool_image_utils.dart';
import 'package:tianpu_edu/Classes/libs/tool_navigation_utils.dart';
import 'package:tianpu_edu/Classes/libs/tool_text_utils.dart';

class HomeDetailsPage extends CatBaseWidgetPage {

  final int ids;

  HomeDetailsPage(this.ids);

  @override
  _HomeDetailsPageState getState() => _HomeDetailsPageState();
}

class _HomeDetailsPageState extends CatBaseWidgetPageState<HomeDetailsPage> {

  HomeDetail _detail;

  HomeCourseModuleList _homeCourseModuleList;

  bool _type = true;

  @override
  void initState() {
    super.initState();
    title = '详情';
    _getHomeDetail();

  }

  Future<void> _getHomeDetail() => ApiUrl.getHomeDetail(null, widget.ids, (success) {
    print(success);

    setState(() {
      _detail = HomeDetail.fromJson(success);
      _getQueryCourseModuleList();
    });
  }, (error) { });

  Future<void> _getQueryCourseModuleList() async{
    Map dict = {
      'commodityId' : _detail.data.commodityId,
      "courseClassId": _detail.data.sourceId,
      "currentVersion": _detail.data.currentVersion
    };
    ApiUrl.getQueryCourseModuleList(null,dict,(success){
      HomeCourseModuleList list = HomeCourseModuleList.fromJson(success);
      setState(() {
        _homeCourseModuleList = list;
      });

    },(error){});
  }


  @override
  Widget buildWidget(BuildContext context) {


    if(_detail == null || _detail.data == null){
      // NavigationUtil.pop(context);
      return Container(child: Text('请求中。。。。'),);
    }
    return CustomScrollView(
      slivers: [
        SliverToBoxAdapter(child: Image.network(_detail.data.pcIconInfo,width: screenSizeW(context),
          fit: BoxFit.fitWidth,
        ),),
        // SliverToBoxAdapter(child: Container(height: 20,),),
        SliverToBoxAdapter(child: Container(padding: EdgeInsets.all(12),
            child: Text(_detail.data.commodityName,style: textStyle(),))),

        // SizedBox(height: 20,),
        // SliverToBoxAdapter(child: Container(height: 20,),),
        SliverToBoxAdapter(
          child: Container(padding: EdgeInsets.only(left: 12,right: 12,bottom: 12),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('¥ ${_detail.data.price}',style: textStyle(color: Colors.red),),
                Text('已报名学员${_detail.data.purchaseNumber}人',style: textStyle(color: textColor),),
              ],
            ),
          ),
        ),

        SliverToBoxAdapter(child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Container(color: Colors.white, width: size.width / 2,
                child: TextButton(
                  //设置水波纹颜色
                    style: ButtonStyle( overlayColor: MaterialStateProperty.all(Colors.white),
                      backgroundColor: MaterialStateProperty.resolveWith((states) {
                        //设置按下时的背景颜色
                        if (states.contains(MaterialState.pressed)) {
                          return Colors.blue[200];
                        }
                        //默认不使用背景颜色
                        return Colors.red[200];
                      }),
                    ),
                    //背景颜色

                    onPressed: (){
                    setState(() {
                      _type = true;
                    });

                  }, child: Text('详情',style: TextStyle(color: _type != true ? Colors.black : mainColor),))),
            Container(color: Colors.white, width: size.width / 2,
                child: TextButton(style: ButtonStyle( overlayColor: MaterialStateProperty.all(Colors.white),),
                    onPressed: (){
                    setState(() {
                      _type = false;
                    });

                  }, child: Text('课表',style: TextStyle(color: _type == true ? Colors.black : mainColor),))),
          ],
        ),),

        // SliverToBoxAdapter(child: Container(height: 20,),),
        _type == true
            ? SliverToBoxAdapter(child: Image.network(_detail.data.pcDetailIconInfo))
            : SliverToBoxAdapter(child: HomeExpansionTileList(homeCourseModuleList: _homeCourseModuleList,) ),
        
      ],
    );
  }



}

